package com.supreme.aspectserver.controller;

import com.supreme.aspectserver.service.simple.LockService;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = "lock")
public class LockController {

    private LockService lockService1;
    private LockService lockService2;

    @PostConstruct
    public void initLockController() {
        lockService1 = new LockService();
        lockService2 = new LockService();
    }

    @GetMapping(value = "syncLockMethod")
    public AjaxResp syncLockMethod(@RequestParam(value = "index", defaultValue = "1") Integer index) {
        CompletableFuture.runAsync(() -> {
            switch (index) {
                case 1:
                    lockService1.syncLockMethod(index);
                    break;

                case 2:
                    lockService2.syncLockMethod(index);
                    break;
            }
        });
        return AjaxResp.instance();
    }

    @GetMapping(value = "syncStaticLockMethod")
    public AjaxResp syncStaticLockMethod(@RequestParam(value = "index", defaultValue = "1") Integer index) {
        CompletableFuture.runAsync(() -> {
            LockService.syncStaticLockMethod(index);
        });
        return AjaxResp.instance();
    }

    @GetMapping(value = "syncObjectLockMethod")
    public AjaxResp syncObjectLockMethod(@RequestParam(value = "index", defaultValue = "1") Integer index) {
        CompletableFuture.runAsync(() -> {
            switch (index) {
                case 1:
                    lockService1.syncObjectLockMethod(index);
                    break;

                case 2:
                    lockService2.syncObjectLockMethod(index);
                    break;
            }
        });
        return AjaxResp.instance();
    }

    @GetMapping(value = "syncStaticObjectLockMethod")
    public AjaxResp syncStaticObjectLockMethod(@RequestParam(value = "index", defaultValue = "1") Integer index) {
        CompletableFuture.runAsync(() -> {
            LockService.syncStaticLockMethod(index);
        });
        return AjaxResp.instance();
    }
}
